package il.ac.tau.cvws.scrabblesidekick.client.app.controller;

import il.ac.tau.cvws.scrabblesidekick.client.engine.CalibrationEngine;
import il.ac.tau.cvws.scrabblesidekick.client.engine.storage.DataKeyStorage;
import il.ac.tau.cvws.scrabblesidekick.client.ui.CalibrationActivity;
import il.ac.tau.cvws.scrabblesidekick.client.ui.ScrabbleSidekickActivity;
import il.ac.tau.cvws.scrabblesidekick.client.ui.constant.UserMessageConstant;
import il.ac.tau.cvws.scrabblesidekick.shared.datatype.ImageData;
import il.ac.tau.cvws.scrabblesidekick.shared.datatype.StorageDataKey;

/* This class is intended as a controller/bridge between the upper platform's
 * UI controller (CalibrationActivity) and the lower engine's logic (CalibrationEngine).
 */
public class CalibrationController extends CameraController 
{
	public CalibrationController(ScrabbleSidekickActivity activity)
	{
		super(activity);
	}
	
	@Override
	protected CalibrationEngine createEngine()
	{
		return new CalibrationEngine(this);
	}
	
	/* An image was snapped, attempt to calibrate the device.
	 */
	@Override
	public void internalHandleHighQualityImage(int screenWidth, int screenHeight, ImageData image)
	{
		((CalibrationEngine)_engine).handleCalibration(screenWidth, screenHeight, image); 
	}
	
	/* Calibration was successful, store the newly created storage key in the key-storage,
	 * and inform the activity of the success.
	 */
	public void handleCalibrationSuccess(StorageDataKey key)
	{
		try
		{
			DataKeyStorage.storeDataKey(key);
			
			((CalibrationActivity)_activity).handleSuccess();
		}
		catch (Exception e)
		{
			handleFailure(UserMessageConstant.CALIBRATION_FAILURE);
		}
	}
}
